//
//  RBHTTPManager.h
//  SaltyHeart
//
//  Created by Lan on 2017/7/18.
//  Copyright © 2017年 LongCai. All rights reserved.
/*
 现在打印/加密就做了NSDictionary,加密还没做
 */

//DEBUG模式下,是否打印网络请求
#define kRBHTTPManagerLog YES
//是否进行加密传输
#define kRBSecurityTransport NO

#import <Foundation/Foundation.h>

/*
 * 两种回调:cacheBlock/netBlock
 * 除了RBHTTPManagerCacheGetPolicyCacheBeforeRequest可能回调两次
 * 其他都只回调一次
 */
typedef NS_ENUM(NSUInteger, RBHTTPManagerCacheGetPolicy)
{
    RBHTTPManagerCacheGetPolicyRequestOnly = 0,/**<不要缓存回调*/
    RBHTTPManagerCacheGetPolicyCacheOnly,/**<只要缓存,不发请求*/
    RBHTTPManagerCacheGetPolicyCacheElseRequest,/**<首先调缓存,没有的话请求*/
    RBHTTPManagerCacheGetPolicyCacheBeforeRequest,/**<数据返回来之前就给缓存 */
    RBHTTPManagerCacheGetPolicyCacheAfterRequest,/**<如果返回有error给缓存 */
};

@interface RBHTTPManager : NSObject

/**
 * 请求过期时长,默认30s
 */
@property(nonatomic, assign) float requestTimeoutInterval;

/**
 * 是否在当前对象销毁时取消进行中的请求,默认为YES
 */
@property(nonatomic, assign) BOOL cancelRequestOnDealloc;

/**
 * 请求是否加载中
 */
@property(nonatomic, assign, readonly) BOOL loading;

/**
 * 设置YES,强制不进行加密传输
 */
@property(nonatomic, assign) BOOL ignoreSecurityTransport;

/**
 * 取消当前所有请求
 */
- (void)cancelTasks;

/**
 * 取消某一请求,taskIdentifier是在请求时返回的
 */
- (void)cancelTaskWithIdentifier: (NSString *)taskIdentifier;

/** 
 *  POST/GET请求
 *
 *  @path: 请求路径 eg: http://127.0.0.1/register.php
 *
 *  @parameters: 请求参数,支持2种类型
 *  1.字典 eg: @{@"name": @"jack", @"password": @"12345"}
 *  2.模型 eg:User *user = [User new];user.name = @"jack";user.passowrd=@"12345"
 *           内部用YYModel转字典,改参数名等操作参照YYModel说明文档,建议使用
 *
 *  @responseClass: 返回数据自动转模型 eg: [MyResponse class]
 *
 *  @cancelRequest: 是否取消其他进行中的请求,默认为NO
 *
 *  @cacheGetPolicy:分5种策略
 *  1.RBHTTPManagerCacheGetPolicyRequestOnly
 *  不调取缓存,completionBlock回调
 *  2.RBHTTPManagerCacheGetPolicyCacheOnly
 *  只要缓存,不发请求,cacheGetBlock回调
 *  3.RBHTTPManagerCacheGetPolicyCacheElseRequest
 *  首先调缓存,有的话cacheGetBlock回调,没有的话请求completionBlock回调
 *  4.RBHTTPManagerCacheGetPolicyCacheBeforeRequest
 *  如果有缓存,cacheGetBlock回调,然后请求completionBlock回调
 *  5.RBHTTPManagerCacheGetPolicyCacheAfterRequest
 *  如果请求失败,并且cache不为空,cacheGetBlock回调,否则completionBlock回调
 *
 *  @cacheGetBlock:
 *  只返回数据
 *
 *  @completionBlock: 返回数据(对于成功/失败的定义不绝对,所以缩减成一个回调,自行判断或者二次封装)
 *  1.response 包含headers等参数
 *  2.responseObject 如果没有指定responseClass返回字典,指定了responseClass返回模型
 *  3.error 错误信息
 *  4.返回 业务端自行判断,决定要不要缓存,YES即经验校验后决定缓存数据
 *
 *  @return 请求ID,供取消用
 */
- (NSString *)postWithURLPath: (NSString *)path
                   parameters: (id)parameters
            responseDataClass: (Class)responseClass
                cancelRequest: (BOOL)cancelRequest
               cacheGetPolicy: (RBHTTPManagerCacheGetPolicy)cacheGetPolicy
                cacheGetBlock: (void(^)(id responseObjecta))cacheGetBlock
              completionBlock: (BOOL(^)(NSHTTPURLResponse *response, id responseObject, NSError * error))completionBlock;

/**
 * 如果自己拼了path就不要传parameters,
 * 传了parameters就不要手动拼path
 */
- (NSString *)getWithURLPath: (NSString *)path
                  parameters: (id)parameters
           responseDataClass: (Class)responseClass
               cancelRequest: (BOOL)cancelRequest
              cacheGetPolicy: (RBHTTPManagerCacheGetPolicy)cacheGetPolicy
               cacheGetBlock: (void(^)(id responseObjecta))cacheGetBlock
             completionBlock: (BOOL(^)(NSHTTPURLResponse *response, id responseObject, NSError * error))completionBlock;

/**
 * 小文件上传
 * image/jpeg
 */
- (NSString *)postWithPath: (NSString *)path
                parameters:(id)parameters
                  fileData:(NSArray *)dataArray       /**<NSData数组*/
                      name:(NSArray *)nameArray       /**<字段名数组,即key*/
                  fileName:(NSArray *)fileNameArray   /**<文件名数组,如:102.jpg*/
                  mimeType:(NSArray *)mimeTypeArray   /**<mimeType数组*/
       responseDataClass: (Class)responseClass
             cancelRequest: (BOOL)cancelRequest
                  progress: (void(^)(NSProgress * uploadProgress))progressBlock
                completion: (void(^)(NSHTTPURLResponse *response, id responseObject, NSError * error))completionBlock;

@end
